/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred.gui.lernen;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.mordred.Frage;
import de.mordred.FragenPreSelector;
import de.mordred.Main;
import de.mordred.gui.FixedPanel;
import de.mordred.gui.TopLeiste;
import de.mordred.gui.VerticalFlowLayout;
import de.mordred.gui.einstellungen.Einstellungen;

public class JahrSelection extends FixedPanel
{
	private static final long serialVersionUID = -8142760422379539037L;

	// Multiselection

	private JLabel _anzahlFragen = new JLabel("Fragen: 0");
	private ArrayList<Frage> _fragentotal;
	private ArrayList<Frage> _fragenselected = new ArrayList<Frage>();

	public JahrSelection(String fach, ArrayList<String> selectedThemen)
	{
		_fragentotal = FragenPreSelector.getFragenByThemen(FragenPreSelector.getFragenByFach(fach),
				selectedThemen);

		_anzahlFragen.setText("Fragen: 0 / " + _fragentotal.size());

		this.setLayout(new BorderLayout());
		TopLeiste topleiste = new TopLeiste(new ThemaSelection(fach, Main.getThemenFürFach(fach)),
				getTitel(), null);

		this.add(topleiste, BorderLayout.NORTH);

		JPanel maincenter = new JPanel(new BorderLayout());
		this.add(maincenter, BorderLayout.CENTER);
		JPanel top = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		top.add(_anzahlFragen);

		final JPanel center = new JPanel(new VerticalFlowLayout());
		maincenter.add(center, BorderLayout.CENTER);
		maincenter.add(top, BorderLayout.NORTH);

		for (final int x : Main.getExaminaJahre())
		{
			String jahr = x % 10 == 3 ? x / 100 + " Frühjahr" : x / 100 + " Herbst";

			final JCheckBox box = new JCheckBox(jahr);
			center.add(box);
			box.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent e)
				{
					ArrayList<Integer> list = new ArrayList<Integer>();
					list.add(x);
					if (box.isSelected())
					{
						// hinzufügen
						_fragenselected.addAll(FragenPreSelector
								.getFragenByJahr(_fragentotal, list));
					}
					else
					{
						// entfernen
						_fragenselected.removeAll(FragenPreSelector.getFragenByJahr(_fragentotal,
								list));
					}
					updateTotal();
				}
			});
		}

		JButton start = new JButton("Start");
		start.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				Main.setNextPanel(new LernenFrageGui(_fragenselected, Einstellungen.getInstance()
						.isRandom()));
			}
		});
		topleiste.add(start);

	}

	@Override
	public String getTitel()
	{
		return "Examina";
	}

	private void updateTotal()
	{
		_anzahlFragen.setText("Fragen: " + _fragenselected.size() + " / " + _fragentotal.size());
	}

}
